package GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import MultiThreaded.SimulationBean;
import MultiThreaded.SimulationController;


public class View extends JFrame {
	
	public JLabel l1;
	public JComboBox CallorPut;
	public JLabel l2;
	public JTextField stockPrice;
	public JLabel l3;
	public JTextField strikePrice;
	public JLabel l4;
	public JComboBox maturity;
	public JLabel l5;
	public JTextField discountRate;
	public JLabel l6;
	public JComboBox carryRate;
	public JLabel l7;
	public JTextField volatility;
	public JLabel l8;
	public JComboBox nbSteps;
	public JLabel l9;
	public JComboBox nbSimulations;
	
	public JPanel setPanel;
	public JButton set1;
	public JButton set2;
	public JButton set3;
	public JButton set4;
	public JButton set5;
	
	
	public JPanel panel;
	
	public JPanel backgroundPanel;
	
	public JButton button;
	
	public Double result;
	public JLabel res;
	
	public List setslist;
	
	
	public  JPanel getPanel() {
		return panel;
	}




	public void setPanel(JPanel panel) {
		this.panel = panel;
	}




	public View () {
		super();
		setTitle("Monte Carlo"); 
		setSize(600,600);
		
		//initialization of the backgroundPanel
		backgroundPanel=new JPanel(new FlowLayout());
		this.add(backgroundPanel);
		
		setPanel=new JPanel();
		setPanel.setPreferredSize(new Dimension(600,20));
		setPanel.setLayout(new GridLayout(1,5));		

		
		panel=new JPanel();	
		panel.setPreferredSize(new Dimension(600, 580));
		panel.setLayout(new GridLayout(15,1));
		
		
		
		//Initialization of setPanel
				
		set1=new JButton("Set 1");
		set2=new JButton("Set 2");
		set3=new JButton("Set 3");
		set4=new JButton("Set 4");
		set5=new JButton("Set 5");
		setPanel.add(set1);
		setPanel.add(set2);
		setPanel.add(set3);
		setPanel.add(set4);
		setPanel.add(set5);

		setContentPane(setPanel);
	

		
		//Initialization of the form
		l1=new JLabel("Call (c) or Put (p)? ");
		CallorPut=new JComboBox();
		l2=new JLabel("Stock Price: ");
		stockPrice=new JTextField(20);
		l3=new JLabel("Strike Price: ");
		strikePrice=new JTextField(20);
		l4=new JLabel("Time to maturity (in years): ");
		maturity=new JComboBox();
		l5=new JLabel("Discount rate: ");
		discountRate=new JTextField(20);
		l6=new JLabel("Cost of Carry Rate (in percentage): ");
		carryRate=new JComboBox();
		l7=new JLabel("Volatility: ");
		volatility=new JTextField(20);
		l8=new JLabel("Number of Steps to execute for each price path: ");
		nbSteps=new JComboBox();
		l9=new JLabel("Number of simulations to run: ");
		nbSimulations=new JComboBox();
		
		
		panel.add(l1);
		panel.add(CallorPut);
		panel.add(l2);
		panel.add(stockPrice);
		panel.add(l3);
		panel.add(strikePrice);
		panel.add(l4);
		panel.add(maturity);
		panel.add(l5);
		panel.add(discountRate);
		panel.add(l6);
		panel.add(carryRate);
		panel.add(l7);
		panel.add(volatility);
		panel.add(l8);
		panel.add(nbSteps);
		panel.add(l9);
		panel.add(nbSimulations);
		
		
		//Retrieving the values for the combo boxes from a file
		BufferedReader br = null;
		String line = "";
		String cvsSplitBy = ",";
	
	try {
		int a=1; 
		br = new BufferedReader(new FileReader("values.txt"));
		while ((line = br.readLine()) != null) {
			
			String[] values = line.split(cvsSplitBy);
			
			if(a==1) {
					for(String val: values) {
						CallorPut.addItem(val);						
					}					
			}			
			if(a==2) {
				for(String val: values) {
					maturity.addItem(val);						
				}					
			}
			if(a==3) {
				for(String val: values) {
					carryRate.addItem(val);						
				}					
			}
			if(a==4) {
				for(String val: values) {
					nbSteps.addItem(val);						
				}					
			}
			if(a==5) {
				for(String val: values) {
					nbSimulations.addItem(val);						
				}					
			}
			a++;
			
		
		}
		
	} catch (FileNotFoundException e1) {
		e1.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		if (br != null) {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	
	button= new JButton("Run the simulation");
	panel.add(button);

	setContentPane(panel);
	
	backgroundPanel.add(setPanel);
	backgroundPanel.add(panel);
	setContentPane(backgroundPanel);
	
	
	button.addActionListener(new ActionListener () {
		public void actionPerformed(ActionEvent e) {

			//add a new JLabel to display the result of the simulation
			JLabel resultat= new JLabel();
			panel.add(resultat);
			
			//create a simulation object
			SimulationBean simbean= new SimulationBean(CallorPut.getSelectedItem().toString(), Double.parseDouble(stockPrice.getText()), Double.parseDouble(strikePrice.getText()), Double.parseDouble(maturity.getSelectedItem().toString()),
			Double.parseDouble(discountRate.getText()), Double.parseDouble(carryRate.getSelectedItem().toString()), Double.parseDouble(volatility.getText()), Integer.parseInt(nbSteps.getSelectedItem().toString()), Integer.parseInt(nbSimulations.getSelectedItem().toString()),
			resultat);
			
			//run the simulation
			result=SimulationController.runSim(simbean);
					
		}
	});
	
	// Store the 5 sets from the file in an arraylist
	setslist=new ArrayList();
	BufferedReader buffer = null;
	String line1 = "";
	String cvsSplitBy1 = ",";

	
		try {
			 
			buffer = new BufferedReader(new FileReader("sets.txt"));
			while ((line1 = buffer.readLine()) != null) {

				String[] setline = line1.split(cvsSplitBy1);
				setslist.add(new SimulationBean(setline[0], Double.parseDouble(setline[1]), Double.parseDouble(setline[2]), Double.parseDouble(setline[3]), Double.parseDouble(setline[4]), Double.parseDouble(setline[5]), Double.parseDouble(setline[6]), Integer.parseInt(setline[7]),
						Integer.parseInt(setline[8]), res));
			}
			
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	

	//when i click on set1 it will show the set of arguments
	set1.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			CallorPut.setSelectedItem(((SimulationBean)setslist.get(0)).getCallPutFlag());
			stockPrice.setText(((SimulationBean)setslist.get(0)).getS().toString());
			strikePrice.setText(((SimulationBean)setslist.get(0)).getX().toString());
			maturity.setSelectedItem(((SimulationBean)setslist.get(0)).getT().toString());
			discountRate.setText(((SimulationBean)setslist.get(0)).getR().toString());
			carryRate.setSelectedItem(((SimulationBean)setslist.get(0)).getB().toString());
			volatility.setText(((SimulationBean)setslist.get(0)).getV().toString());
			nbSteps.setSelectedItem(String.valueOf(((SimulationBean)setslist.get(0)).getnSteps()));
			nbSimulations.setSelectedItem(String.valueOf(((SimulationBean)setslist.get(0)).getnSimulations()));
			
		}
	});
	
	set2.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			CallorPut.setSelectedItem(((SimulationBean)setslist.get(1)).getCallPutFlag());
			stockPrice.setText(((SimulationBean)setslist.get(1)).getS().toString());
			strikePrice.setText(((SimulationBean)setslist.get(1)).getX().toString());
			maturity.setSelectedItem(((SimulationBean)setslist.get(1)).getT().toString());
			discountRate.setText(((SimulationBean)setslist.get(1)).getR().toString());
			carryRate.setSelectedItem(((SimulationBean)setslist.get(1)).getB().toString());
			volatility.setText(((SimulationBean)setslist.get(1)).getV().toString());
			nbSteps.setSelectedItem(String.valueOf(((SimulationBean)setslist.get(1)).getnSteps()));
			nbSimulations.setSelectedItem(String.valueOf(((SimulationBean)setslist.get(1)).getnSimulations()));
			
		}
	});
	
	set3.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			CallorPut.setSelectedItem(((SimulationBean)setslist.get(2)).getCallPutFlag());
			stockPrice.setText(((SimulationBean)setslist.get(2)).getS().toString());
			strikePrice.setText(((SimulationBean)setslist.get(2)).getX().toString());
			maturity.setSelectedItem(((SimulationBean)setslist.get(2)).getT().toString());
			discountRate.setText(((SimulationBean)setslist.get(2)).getR().toString());
			carryRate.setSelectedItem(((SimulationBean)setslist.get(2)).getB().toString());
			volatility.setText(((SimulationBean)setslist.get(2)).getV().toString());
			nbSteps.setSelectedItem(String.valueOf(((SimulationBean)setslist.get(2)).getnSteps()));
			nbSimulations.setSelectedItem(String.valueOf(((SimulationBean)setslist.get(2)).getnSimulations()));
			
		}
	});
	
	set4.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			CallorPut.setSelectedItem(((SimulationBean)setslist.get(3)).getCallPutFlag());
			stockPrice.setText(((SimulationBean)setslist.get(3)).getS().toString());
			strikePrice.setText(((SimulationBean)setslist.get(3)).getX().toString());
			maturity.setSelectedItem(((SimulationBean)setslist.get(3)).getT().toString());
			discountRate.setText(((SimulationBean)setslist.get(3)).getR().toString());
			carryRate.setSelectedItem(((SimulationBean)setslist.get(3)).getB().toString());
			volatility.setText(((SimulationBean)setslist.get(3)).getV().toString());
			nbSteps.setSelectedItem(String.valueOf(((SimulationBean)setslist.get(3)).getnSteps()));
			nbSimulations.setSelectedItem(String.valueOf(((SimulationBean)setslist.get(3)).getnSimulations()));
			
		}
	});

	set5.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			CallorPut.setSelectedItem(((SimulationBean)setslist.get(4)).getCallPutFlag());
			stockPrice.setText(((SimulationBean)setslist.get(4)).getS().toString());
			strikePrice.setText(((SimulationBean)setslist.get(4)).getX().toString());
			maturity.setSelectedItem(((SimulationBean)setslist.get(4)).getT().toString());
			discountRate.setText(((SimulationBean)setslist.get(4)).getR().toString());
			carryRate.setSelectedItem(((SimulationBean)setslist.get(4)).getB().toString());
			volatility.setText(((SimulationBean)setslist.get(4)).getV().toString());
			nbSteps.setSelectedItem(String.valueOf(((SimulationBean)setslist.get(4)).getnSteps()));
			nbSimulations.setSelectedItem(String.valueOf(((SimulationBean)setslist.get(4)).getnSimulations()));
			
		}
	});

}
}
